﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy3_4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                // 读取输入并转换为对应类型
                int wheels = int.Parse(txtWheels.Text);
                float weight = float.Parse(txtWeight.Text);
                float maxLoad = float.Parse(txtMaxLoad.Text);
                float oilPerKm = float.Parse(txtOilPerKm.Text);
                float nowLoad = float.Parse(txtNowLoad.Text);
                float runKm = float.Parse(txtRunKm.Text);

                // 创建Truck对象并赋值
                Truck t = new Truck();
                t.SetVehicle(wheels, weight, maxLoad, oilPerKm);

                // 判断是否超重
                if (t.Overload(nowLoad))
                {
                    MessageBox.Show("对不起！卡车已超重，请卸一些货物。");
                    txtNowLoad.Focus();
                }
                else
                {
                    float oil = t.OilConsumption(runKm);
                    MessageBox.Show($"卡车正在行驶中…\n现已耗油 {oil} 升！");
                    t.GetVehicle(); // 显示卡车详细信息
                }
            }
            catch (FormatException)
            {
                MessageBox.Show("请输入有效的数字！", "格式错误");
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误：" + ex.Message, "错误");
            }
        }
    }
}
